﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace NovoUI.UserCtrl
{
    public partial class OutputLogCtrl : UserControl
    {
        public OutputLogCtrl()
        {
            InitializeComponent();
            this.rtxt_OutputLog.ZoomFactor = 1.2f;

            //rtxt_OutputLog.SelectionBullet = false;
            rtxt_OutputLog.SelectionIndent = 8;
            //rtxt_OutputLog.SelectionHangingIndent = 4;
        }

        public string Log
        { get { return this.rtxt_OutputLog.Text; } }

        public void AppendLog(string strLog)
        {
            int length = strLog.Length;
            int beginIndex = rtxt_OutputLog.Text.Length;
            rtxt_OutputLog.AppendText(strLog + "\r\n");
            //rtxt_OutputLog.ForeColor = Color.Chartreuse;
            //rtxt_OutputLog.Select(beginIndex, length);
            //rtxt_OutputLog.SelectionColor = fontColor;
            rtxt_OutputLog.Select(rtxt_OutputLog.Text.Length, 0);//.SelectedText = "";
            rtxt_OutputLog.ScrollToCaret();
            rtxt_OutputLog.Refresh();
        }

        public void AppendLog(string strLog, Color fontColor)
        {
            int length = strLog.Length;
            int beginIndex = rtxt_OutputLog.Text.Length;
            rtxt_OutputLog.AppendText(strLog + "\r\n");
            //rtxt_OutputLog.ForeColor = Color.Chartreuse;
            rtxt_OutputLog.Select(beginIndex, length);
            rtxt_OutputLog.SelectionColor = fontColor;
            rtxt_OutputLog.Select(rtxt_OutputLog.Text.Length, 0);//.SelectedText = "";
            rtxt_OutputLog.ScrollToCaret();
            rtxt_OutputLog.Refresh();
        }

        public void AppendLog(string strLog, Color fontColor, bool newLineBefore, bool newLineAfter)
        {
            int length = strLog.Length;
            int beginIndex = rtxt_OutputLog.Text.Length;
            Color colorBackup = rtxt_OutputLog.SelectionColor;
            
            if (newLineBefore)
                rtxt_OutputLog.AppendText("\r\n");

            if (newLineAfter)
                rtxt_OutputLog.AppendText(strLog + "\r\n");
            else
                rtxt_OutputLog.AppendText(strLog);

            //rtxt_OutputLog.ForeColor = Color.Chartreuse;
            rtxt_OutputLog.Select(beginIndex, length);
            rtxt_OutputLog.SelectionColor = fontColor;
            rtxt_OutputLog.Select(rtxt_OutputLog.Text.Length, 0);//.SelectedText = "";
            rtxt_OutputLog.SelectionColor = colorBackup;

            rtxt_OutputLog.ScrollToCaret();
            rtxt_OutputLog.Refresh();
        }

        private void contextMenuStrip_Copy_MouseUp(object sender, MouseEventArgs e)
        {
            this.rtxt_OutputLog.Copy();
        }

        private void contextMenuStrip_Paste_Click(object sender, EventArgs e)
        {
            this.rtxt_OutputLog.Paste();
        }

        private void contextMenuStrip_Clear_MouseUp(object sender, MouseEventArgs e)
        {
            this.rtxt_OutputLog.Text = null;
            //解决Scroll Bar的刷新问题。
            this.rtxt_OutputLog.ScrollBars = RichTextBoxScrollBars.None;
            this.rtxt_OutputLog.ScrollBars = RichTextBoxScrollBars.Both;
        }

        private void contextMenuStrip_SelAll_Click(object sender, EventArgs e)
        {
            this.rtxt_OutputLog.SelectAll();
        }

        private void rtxt_OutputLog_TextChanged(object sender, EventArgs e)
        {
            //if (rtxt_OutputLog.TextLength > (int)(rtxt_OutputLog.MaxLength * 0.5))
            ////if (rtxt_OutputLog.TextLength > 100)
            //{
            //    rtxt_OutputLog.Text = rtxt_OutputLog.Text.Substring(rtxt_OutputLog.Text.Length - (int)(rtxt_OutputLog.MaxLength * 0.5), (int)(rtxt_OutputLog.MaxLength * 0.5));
            //    rtxt_OutputLog.ForeColor = Color.Black;
            //    //rtxt_OutputLog.Text = rtxt_OutputLog.Text.Substring(rtxt_OutputLog.Text.Length - 100, 100);
            //    rtxt_OutputLog.SelectionStart = rtxt_OutputLog.Text.Length;
            //    rtxt_OutputLog.ScrollToCaret();                    
            //    rtxt_OutputLog.Refresh();

            //}
        }

        private void saveAsToolStripMenuItem_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                SaveFileDialog savefiledlg = new SaveFileDialog();
                savefiledlg.Title = "Saving script command...";
                savefiledlg.Filter = "Config file(*.log)|*.log|Text file(*.txt)|*.txt";
                savefiledlg.RestoreDirectory = true;

                //if (!Directory.Exists(Environment.CurrentDirectory + "\\log"))
                //    Directory.CreateDirectory(Environment.CurrentDirectory + "\\log");
                //savefiledlg.InitialDirectory = Environment.CurrentDirectory + "\\log";

                string filename = "";
                if (savefiledlg.ShowDialog() == DialogResult.OK)
                {
                    filename = savefiledlg.FileName;
                }
                else
                    return;

                StreamWriter sw = File.CreateText(filename);
                sw.Write(this.rtxt_OutputLog.Text);

                sw.Close();
            }
            catch(Exception ex) 
            {
                MessageBox.Show("Save log failed! Error:{0}", ex.Message);
            }
        }

        private void saveToolStripMenuItem_MouseUp(object sender, MouseEventArgs e)
        {
            string logPath = Environment.CurrentDirectory + "\\log";
            try
            {
                if (!Directory.Exists(logPath))
                    Directory.CreateDirectory(logPath);

                logPath += "\\log_" + DateTime.Now.ToString("d").Replace('/', '-') + ".log";

                StreamWriter sw = new StreamWriter(logPath, true);
                sw.Write(this.rtxt_OutputLog.Text);
                sw.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        /*Boxiong*/
        public void Clear()
        {
            rtxt_OutputLog.Clear();
        }
    }
}
